<template>
	<up-form label-position="left" :model="formData" :rules="rules" ref="dFormRef">
		<div class="form-item" v-for="item in list" :key="item.key">
			<up-form-item :label="item.label" :prop="item.key" @click="item.type==='select'&&open(item)"
				:labelStyle="{fontSize:'28rpx'}" :labelWidth="labelWidth" class="item">
				<template v-if="item.type==='input'">
					<div class="flex-rowr">
						<up-input inputAlign="right" v-model="formData[item.key]" :placeholder="item.placeholder"
							border="none" fontSize='28rpx'></up-input><text
							class="ml10">{{item.unit?item.unit:""}}</text>
					</div>
				</template>
				<template v-if="item.type==='select'">
					<up-input inputAlign="right" v-model="formData[item.key]" disabled disabledColor="#ffffff"
						:placeholder="item.placeholder" border="none"></up-input>
					<template #right>
						<up-icon name="arrow-right"></up-icon>
					</template>
				</template>
				<template v-if="item.type==='radio'">
					<up-radio-group v-model="formData[item.key]" placement="column">
						<div class="flex-rowr">
							<up-radio class="mr16" activeColor="#FFBF60" :customStyle="{marginBottom: '8px'}"
								v-for="key in item.options" :key="key.name" :label="key.name" :name="key.name">
							</up-radio>
						</div>
					</up-radio-group>
				</template>
				<template v-if="item.type==='textarea'">
					<up-textarea v-model="formData[item.key]" :placeholder="item.placeholder" count autoHeight
						:maxlength="item.maxlength"></up-textarea>
				</template>
				<template v-if="item.type==='upload'">
					<up-upload :fileList="item.fileList" @afterRead="afterRead" @delete="deletePic" name="1"
						:multiple="item.multiple" :maxCount="item.maxCount" :accept='item.accept'
						:previewFullImage="item.previewFullImage"></up-upload>
				</template>


			</up-form-item>
		</div>
		<up-button @click="handleSubmit" class="mt20" type="warning" v-if="isFooter">提交</up-button>
	</up-form>
	<up-action-sheet :show="sheet.show" :actions="sheet.actions" :title="sheet.title" :description="sheet.description"
		@close="sheetClose" @select="handleSelect">
	</up-action-sheet>
</template>

<script setup>
	import {
		reactive,
		ref,
		computed
	} from "vue"
	const {
		list,
		labelWidth
	} = defineProps({
		list: {
			type: Array,
			default: () => []
		},
		labelWidth: {
			type: String,
			default: "200rpx"
		},
		isFooter: true
	})
	const emit = defineEmits(['submit'])
	const formData = reactive({
		name: ""
	})
	const sheet = reactive({
		show: false,
		actions: [],
		title: "",
		description: ""
	})
	// 设置校验
	const rules = computed(() => {
		const obj = {}
		list.forEach(item => {
			if (item.rule && item.rule.length > 0)
				obj[item.key] = [...item.rule]
		})
		return obj
	})

	// 开启弹框
	const open = (item) => {
		sheet.show = true
		sheet.actions = item.options
		sheet.title = item.placeholder
		sheet.description = item.description || ""
	}
	// 关闭弹框
	const sheetClose = () => {
		sheet.show = false
	}

	// 选择
	const handleSelect = (val) => {
		console.log(val)
	}
	// 上传
	const afterRead = () => {}
	// 删除上传文件
	const deletePic = () => {}
	const dFormRef = ref(null)
	// 提交
	const handleSubmit = () => {
		dFormRef.value.validate().then(valid => {
			if (valid) {
				submit(formData)
			}
		})
	}
</script>

<style>
	.form-item {
		height: 90rpx;
		padding-top: 10rpx;
		width: 100%;
		border-bottom: 1rpx solid #f5f5f5;
	}

	.flex-rowr {
		display: flex;
		justify-content: flex-end;
		align-items: center;
	}

	.ml10 {
		margin-left: 10rpx;
	}
</style>